package homework;

import java.io.*;
import java.util.*;

/*
分析以下需求，并用代码实现
1.定义学生类,包含姓名(String name),性别(String gender),年龄(int age)三个属性,生成空参有参构造,set和get方法,toString方法
2.键盘录入6个学员信息(录入格式:张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
3.将存有6个学员信息的ArrayList集合对象写入到D:\\StudentInfo.txt文件中
4.读取D:\\StudentInfo.txt文件中的ArrayList对象并遍历打印
5.对ArrayList集合中的6个学生对象进行去重,将去重后的结果利用PrintWriter流写入到E:\\StudentInfo.txt文件中(写入格式：张三-男-25)
*/
public class Test5 {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student>list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            System.out.println("请输入学生信息");
            String s = sc.nextLine();
            String[] arr = s.split(",");
            Student stu = new Student();
            stu.setName(arr[0]);
            stu.setGender(arr[1]);
            stu.setAge(Integer.parseInt(arr[2]));
            list.add(stu);
        }

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Day13\\src\\homework\\StudentInfo.txt"));
        oos.writeObject(list);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day14-code\\StudentInfo.txt"));
        List<Student> list1 = (List)ois.readObject();
        ois.close();

        for (Student student : list1) {
            System.out.println(student);
        }

        Set<Student> set = new HashSet<>(list1);
        BufferedWriter bw = new BufferedWriter(new FileWriter("day14-code\\Student.txt"));
        for (Student stu : set) {
            bw.write(stu.getName()+"-"+stu.getGender()+"-"+stu.getAge());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}


/*        BufferedWriter bw = new BufferedWriter(new FileWriter("Day13\\src\\homework\\StudentInfo.txt"));
        bw.write(String.valueOf(list));
        bw.newLine();
        bw.flush();

        BufferedReader br = new BufferedReader(new FileReader("Day13\\src\\homework\\StudentInfo.txt"));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        bw.close();
        br.close();*/


